package com.nativetalk.service.teafound.search;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.FoundChange.TdNtTeaFoundChangeList;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.dao.TdNtTeaFoundChangeDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 15/12/28.
 */
@Service
public class TeaFoundChangeSearchServiceImpl implements TeaFoundChangeSearchService {
    private static Logger logger = Logger.getLogger(TeaFoundChangeSearchServiceImpl.class);
    @Resource
    private TdNtTeaFoundChangeDao tdNtTeaFoundChangeDao;
    @Override
    public RetInfo findFoundChangeByTeaId(Integer tea_id, int currentPage, int pageSize) {
        String logInfo = this.getClass().getName() + ":findFoundChangeByTeaId:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            int totalPage;//总页数
            int totalRecord = tdNtTeaFoundChangeDao.findTdNtTeaFoundChangeSize(tea_id);//总条数
            if (totalRecord % pageSize == 0) {
                totalPage = totalRecord/pageSize;
            } else {
                totalPage = (totalRecord/pageSize) + 1;
            }
            if (currentPage >= totalPage) {
                currentPage = totalPage;
            }
            if (currentPage <= 1) {
                currentPage = 1;
            }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("tea_id",tea_id);
            map.put("currentPage",(currentPage-1)*10);
            map.put("pageSize",pageSize);

            List<TdNtTeaFoundChangeList> foundLists = tdNtTeaFoundChangeDao.findTdNtTeaFoundChangePage(map);
            if (foundLists == null || foundLists.size() <= 0) {
                Map<String, Object> result = new HashMap<>();
                result.put("foundLists", foundLists);
                result.put("totalPage", totalPage);
                result.put("totalRecord", totalRecord);
                result.put("currentPage", currentPage);
                retInfo.setObj(result);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip(ValidationMessages.getValue("TEACHER.FOUND.RESULT.NULL"));
            } else {
                for (int i = 0; i <foundLists.size() ; i++) {
                    TdNtTeaFoundChangeList tdNtTeaFoundChangeList = foundLists.get(i);
                    if(3 == tdNtTeaFoundChangeList.getType()){
                        tdNtTeaFoundChangeList.setTfchg_change_name("Others");
                    }
                }
                Map<String, Object> result = new HashMap<>();
                result.put("foundLists", foundLists);
                result.put("totalPage", totalPage);
                result.put("totalRecord", totalRecord);
                result.put("currentPage", currentPage);
                retInfo.setObj(result);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取资金变动列表成功");
            }
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
